package com.dvg.data;

import java.util.Hashtable;

import com.dvg.scores.ScoreModuleParameters;

import net.rim.device.api.system.PersistentObject;
import net.rim.device.api.system.PersistentStore;

public final class Storage {
	
	private static Hashtable storageParams;
	
	private static PersistentObject storage;
	
	private static final String USER_NAME = "user_name";
	
	static{
		storage = PersistentStore.getPersistentObject(ScoreModuleParameters.APP_ID);
	}
	
	public Storage(){
		storageParams = new Hashtable();
		Storage.getData();
	}
	
	private static void getData() {
		synchronized (storage) {
			if ((Hashtable) storage.getContents() == null){
				Storage.setDefaultSettings();
				storage.setContents(storageParams);
				storage.commit();
			} else {
				storageParams = (Hashtable) storage.getContents();
			}
		}
	}

	private static void setDefaultSettings() {
		Storage.setUserName("");
	}
	
	public static void setUserName(String userName) {
		storageParams.remove(USER_NAME);
		storageParams.put(USER_NAME, userName);
		saveData();
	}
	
	public static String getUserName() {
		return storageParams.get(USER_NAME).toString();
	}
	
	private static void saveData() {
		synchronized (storage) {
			storage.setContents(storageParams);
			storage.commit();
		}
	}
}
